package com.dj.mingkong.admin.biz.repository.database.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.admin.biz.controller.system.vo.request.UserLoginLogQueryVo;
import com.dj.mingkong.admin.biz.util.WrapperUtil;
import com.dj.mingkong.common.constant.CommonConstants;
import com.dj.mingkong.common.infrastructure.base.dto.query.QueryVo;
import com.dj.mingkong.common.infrastructure.base.repository.BaseDatabaseRepository;
import com.dj.mingkong.common.infrastructure.eo.database.system.UserLoginLogEo;
import com.dj.mingkong.admin.biz.repository.database.system.mapper.UserLoginLogMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Arrays;

/**
 * 用户登录日志repository
 *
 * @author zenglonghui
 * @date: 2022/12/7 10:30
 */
@Repository
public class UserLoginLogRepository extends BaseDatabaseRepository<UserLoginLogMapper, UserLoginLogEo, QueryVo> {
    public Page<UserLoginLogEo> listPageByVo(UserLoginLogQueryVo queryVo) {
        final QueryWrapper<UserLoginLogEo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotBlank(queryVo.getUsername()),
                        UserLoginLogEo::getUsername, queryVo.getUsername())
                .like(StringUtils.isNotBlank(queryVo.getLoginIp()),
                        UserLoginLogEo::getLoginIp, queryVo.getLoginIp())
                .in(StringUtils.isNotBlank(queryVo.getLoginType()), UserLoginLogEo::getLoginType,
                        Arrays.asList(queryVo.getLoginType().split(CommonConstants.SPLIT_COMMA)))
                .in(StringUtils.isNotBlank(queryVo.getAppCode()), UserLoginLogEo::getAppCode,
                        Arrays.asList(queryVo.getAppCode().split(CommonConstants.SPLIT_COMMA)))
                .ge(StringUtils.isNotBlank(queryVo.getStartDate()), UserLoginLogEo::getLoginTime,
                        queryVo.getStartDate())
                .le(StringUtils.isNotBlank(queryVo.getEndDate()), UserLoginLogEo::getLoginTime,
                        queryVo.getEndDate());
        WrapperUtil.buildSort(queryWrapper, queryVo.getSort());
        return listPage(queryWrapper, queryVo.getPage(), queryVo.getPageSize());
    }
}
